﻿var EHRValidate = function (option) {
    var config = { mode: "", isEn: "" };
    $.extend(config, option);
    var divImgID = "divVImage";
    var divPhraseID = "divVPhrase";
    var divStepID = "divVStep";
    var index = 0;
    var key = "";
    var list = new Array();
    this.succValidate = 0;
    this.clickSourceID = "";
    var time;
    var succFlag = true;
    var guid = "";
    this.Refresh = function () {
        if (!succFlag) {
            return;
        }
        var url;
        succFlag = false;
        guid = GetGuid(20, 16);
        $("#hidVGuid").val(guid);
        var isEN = false;
        var errObj = $("#errCheckCodeCN");
        var errObj2 = $("#errCheckCodeCNTxt");
        var objDiv = $("#CheckCodeCNdiv");
        //if (config.mode == "1") {
        key = $("#hidAccessKey").val();
        var doType;
        switch (config.mode) {
            case "2":
                doType = "CheckResumeView";
                break;
            case "1":
            case "3":
                doType = "CheckSearchResume";
                break;
            case "4":
                doType = "CheckInboxResumeView";
                break;
            case "5":
            case "7":
                doType = "CheckInboxResume";
                break;
            case "6":
            case "8":
                doType = "CheckCompanyHRResume";
                break;
        }
        url = '../ajax/Validate/PageValidate.aspx?type=getverify&key=' + key + "&guid=" + guid + "&doType=" + doType;
        $("#errCheckCodeCN,#errOther").hide();
        // }
        $("#" + divPhraseID + " i,#" + divImgID + " i").css("backgroundImage", "none");
        var phraseHtml = $("#" + divPhraseID).html();
        $("#" + divPhraseID).html(phraseHtml);
        phraseHtml = $("#" + divImgID).html();
        $("#" + divImgID).html(phraseHtml);
        $("#imgPhrase").attr("src", url);
        $("#" + divPhraseID + " i,#" + divImgID + " i").css("backgroundImage", "url(" + url + ")");
        $("#divLoading").show();
        $('#imgPhrase').load(function () {
            succFlag = true;
            $("#divLoading").hide();
        });
        $("#" + divStepID + " span").hide();
        index = 0;
        list = new Array();
        $("#btnVRefresh").show();
        $("#btnVRefresh").next("span").hide();
        $("#btnValidate").removeClass("on");
        if (typeof (time) != "undefined") {
            clearTimeout(time);
        }
    }
    var GetGuid = function (len, radix) {
        var chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'.split('');
        var uuid = [], i;
        radix = radix || chars.length;
        if (len) {
            for (i = 0; i < len; i++) uuid[i] = chars[0 | Math.random() * radix];
        } else {
            var r;
            uuid[8] = uuid[13] = uuid[18] = uuid[23] = '-';
            uuid[14] = '4';
            for (i = 0; i < 36; i++) {
                if (!uuid[i]) {
                    r = 0 | Math.random() * 16;
                    uuid[i] = chars[(i == 19) ? (r & 0x3) | 0x8 : r];
                }
            }
        }
        return uuid.join('');
    }
    var ShowButton = function () {
        if (index == 4) {
            $("#btnValidate").addClass("on").attr("href", "javascript:void(0)");
        }
        else {
            $("#btnValidate").removeClass("on").removeAttr("href");
        }
    }
    this.Init = function () {
        EHR.ShowDivBox("#divVerifyCode_ch");
        if (config.mode == "2" || config.mode == "4") {
            $("#bt_Close,#bt_Pre,#bt_Next").hide();
        }
        $("#" + divStepID + " span").live("click", function (e) {
            if (!succFlag) {
                return;
            }
            var x = parseInt($(this).attr("class").replace("yz-step", ""));
            for (var i = x; i <= index; i++) {
                list.pop();
            }
            $(this).nextAll("span").hide();
            $(this).hide();
            index = x - 1;
            ShowButton();
        });
        $("#" + divImgID).click(function (e) {
            if (!succFlag) {
                return;
            }
            var event = window.event ? window.event : e;
            var src = event.srcElement ? event.srcElement : event.target;
            if (index < 4 && src.className.toString().indexOf("yz-step") == -1) {
                var scrollTop = document.documentElement.scrollTop == 0 ? document.body.scrollTop : document.documentElement.scrollTop || document.body.scrollTop;
                var scrollLeft = document.documentElement.scrollLeft == 0 ? document.body.scrollLeft : document.documentElement.scrollLeft || document.body.scrollLeft;
                var x = parseInt(event.clientX + scrollLeft - parseInt($(this).offset().left));
                var y = parseInt(event.clientY + scrollTop - parseInt($(this).offset().top));
                if (x < 10) {
                    x = 10;
                }
                else if (x > 320) {
                    x = 320;
                }
                if (y < 10) {
                    y = 10;
                }
                else if (y > 106) {
                    y = 106;
                }
                var flag = true;
                for (var i = 0; i < list.length; i++) {
                    var point = list[i].split(',');
                    if (Math.pow(Math.abs(parseInt(point[0]) - x), 2) + Math.pow(Math.abs(parseInt(point[1]) - y), 2) < 400) {
                        flag = false;
                        break;
                    }
                }
                if (flag) {
                    index++;
                    $("#" + divStepID + " .yz-step" + index).show().css({ "left": x - 10, "top": y - 10 });
                    list.push(x + "," + y);
                    ShowButton();
                }
            }
        });

        $("body").click(function (e) {
            //            if (config.mode == "1") {
            if (ehr.succValidate == 0) {
                //var obj = e.srcElement ? e.srcElement : e.target;
                var event = window.event ? window.event : e;
                if (event.keyCode == 13) {
                    return;
                }
                var obj = event.srcElement ? event.srcElement : event.target;
                if ($(obj).parents("#CheckCodeCNdiv").length > 0 || $(obj).attr("id") == "Login_btnLoginCN") {
                    return;
                }
                $("#btnBeginValidate").html("点击完成验证");
                //$("#divValidateHtml").hide();
            }
            //}
        });
        ehr.Refresh();

        //        if (config.isEn == "1") {
        //            CodeEnLoad();
        //            $("#imgCheckCodeEN").click(function () {
        //                CodeEnLoad();
        //            });
        //        }
    }
    //    var CodeEnLoad = function () {
    //        guid = GetGuid(20, 16);
    //        $("#hidVGuid").val(guid);
    //        $("#imgCheckCodeEN").attr("src", "./CommonPage/RandomNumber.aspx?type=login&guid=" + guid);
    //    }
    this.ValidateResult = function () {
        if (index != 4) {
            return;
        }
        var url;
        //if (config.mode == "1") {
        url = "../ajax/Validate/PageValidate.aspx";
        //}
        var doType;
        switch (config.mode) {
            case "2":
                doType = "CheckResumeView";
                break;
            case "1":
            case "3":
                doType = "CheckSearchResume";
                break;
            case "4":
                doType = "CheckInboxResumeView";
                break;
            case "5":
            case "7":
                doType = "CheckInboxResume";
                break;
            case "6":
            case "8":
                doType = "CheckCompanyHRResume";
                break;
        }
        $.ajax({
            url: url,
            type: "POST",
            async: true,
            dataType: "xml",
            data: "type=checkverift&key=" + key + "&p=" + list.join(";") + "&guid=" + guid + "&doType=" + doType,
            success: function (result) {
                var msgtype = $(result).find("msgtype").eq(0).text();
                var res = $(result).find("result").eq(0).text();
                if (msgtype == "1") {
                    //if (config.mode == "1") {
                    $("#divValidateHtml,#btnBeginValidate").hide();
                    $("#btnEndValidate").show();
                    ehr.succValidate = 1;
                    if (ehr.clickSourceID != "") {
                        $("#" + ehr.clickSourceID).click();
                    }
                    //}
                    //                   if()
                    EHR.CloseDivBox("#divVerifyCode_ch");
                    $("#hidShowCode").val("0");
                    switch (config.mode) {
                        case "1":
                        case "7":
                        case "8":
                            var isShow = $("#hidDisplayType").val();
                            if (isShow == "1") {
                                $("#btnDetailDis").click();
                            }
                            else {
                                $("#btnSampleDis").click();
                            }
                            break;
                        case "2":
                            __doPostBack(hidNameId, '');
                            break;
                        case "3":
                        case "5":
                        case "6":
                            var isShow = $("#hidDisplayType").val();
                            if (isShow == "1") {
                                $("#imgDis2").click();
                            }
                            else {
                                $("#imgDis1").click();
                            }
                            break;
                        case "4":
                            __doPostBack("NeedInit", '');
                            break;
                    }
                }
                else {
                    $("#btnVRefresh").next("span").show();
                    time = setTimeout("ehr.Refresh();", 1000);
                    $("#btnVRefresh").hide();
                    index = 0;
                    ShowButton();
                }
            }
        });
    }
}
//$(function () {
//    ehr.Init();
//    $("#btnBeginValidate").click(function () {
//        ehr.Refresh();
//        $("#divValidateHtml").show();
//        $(this).text("正在验证");
//    });
//    $("#btnValidate").click(function () {
//        ehr.ValidateResult();
//    });
//    $("#btnVRefresh").click(function () {
//        ehr.Refresh();
//    });
//});